﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using UFSSDesktopApp.UFSSservicereference;

namespace UFSSDesktopApp
{
    public partial class FormSubscription : Form
    {
        private UFSSservicereference.UFSSserviceSoapClient ussc;
        private UFSSservicereference.UserCreds ucreds;
        private ArrayList sublist = new ArrayList();
        private ArrayList friendlist = new ArrayList();
        private void refresh()
        {
            try
            {
                sublist.Clear();
                friendlist.Clear();
                lbSubs.Items.Clear();
                UFSSSubscription[] subs;
                subs = ussc.getSubs(ucreds);
                ArrayOfString friends = ussc.getFriends(ucreds);
                for (int i = 0; i < subs.Length; i++)
                    sublist.Add(subs[i]);
                foreach (UFSSSubscription s in sublist)
                    lbSubs.Items.Add(s.subName);
                for (int i = 0; i < friends.Count; i++)
                    friendlist.Add(friends[i]);
                int sublimit = ussc.getSystemLimit("UserSub");
                int subcount = ussc.getUserStat(ucreds, "SubCount");
                pbSubs.Maximum = sublimit;
                pbSubs.Value = subcount;
                lblCurrent.Text = subcount.ToString();
                lblLimit.Text = sublimit.ToString();
                lblRemaining.Text = (sublimit - subcount).ToString();
            }
            catch
            { }
            
        }
        public FormSubscription(UFSSservicereference.UFSSserviceSoapClient sc, UFSSservicereference.UserCreds uc)
        {
            InitializeComponent();
            ussc = sc;
            ucreds = uc;
        }

        private void FormSubscription_Load(object sender, EventArgs e)
        {
            refresh();
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            Form.ActiveForm.Hide();
            FormMain fm = new FormMain(ussc, ucreds);
            fm.Activate();
            fm.Show();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            refresh();
            string subtype = "PUBLIC";
            if(rbPrivate.Checked)
                subtype = "PRIVATE";
            if (ussc.createSub(ucreds, subtype, txtTitle.Text))
                lblStatus.Text = "Subscription succesfully created!";
            else
                lblStatus.Text = "Subscription could not be created.";
        }

        private void lbSubs_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                int selected = lbSubs.SelectedIndex;
                if (selected >= 0)
                {
                    btnDelete.Enabled = true;
                    UFSSSubscription tempsub = (UFSSSubscription)sublist[selected];
                    lblFiles.Text = tempsub.files.ToString();
                    lblPrivacy.Text = tempsub.type;
                    lblSubscribers.Text = tempsub.members.ToString();
                }
                else
                {
                    btnDelete.Enabled = false;
                    lblFiles.Text = "--";
                    lblPrivacy.Text = "--";
                    lblSubscribers.Text = "--";
                }
            }
            catch
            { }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {

            try
            {

                ussc.deleteSub(ucreds, lbSubs.SelectedItem.ToString());
                refresh();
                btnDelete.Enabled = false;
                lblError.Text = "Subscription successfully deleted";
            }
            catch
            {
                lblError.Text = "Error deleting subscription.";
            }
        }

        private void tpSubs_Click(object sender, EventArgs e)
        {

        }

        private void tpCreate_Click(object sender, EventArgs e)
        {

        }

    }
}
